package com.mingqijia.gassafety.authority.security.user;

import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.shared.user.UserBackend;
import com.mingqijia.gassafety.shared.user.UserFactory;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

/**
 * 获取安全方案下的用户
 * @author damon
 */
@Component
public final class SecurityUserBackend implements UserFactory  {

    @Override
    public UserBackend getPrincipal() {
        Authentication authentication = getAuthentication();
        if (authentication instanceof UserBackend) {
            return (UserBackend) authentication;
        }
        if (authentication.getPrincipal() instanceof UserBackend) {
            return (UserBackend) authentication.getPrincipal();
        }
        throw new BizException("用户未登录");
    }
}